//
//  WPAlertView.m
//  wobingwoyi
//
//  Created by WGP on 16/10/8.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "WPAlertView.h"

#define K_SCALE_WIDTH   (([UIScreen mainScreen].bounds.size.width)/375.0)

@interface WPAlertView ()

/** 遮盖 */
@property (nonatomic, strong) UIButton *coverView;
/** 背景View */
@property (nonatomic, strong) UIView *bg_view;

/** 信息提示文字 */
@property (nonatomic, copy) NSString *messageTitle;

@property(nonatomic,strong)NSArray *titles;

/** SGAlertViewBottomViewTypeTwo */
@property (nonatomic, strong) UIView *bottomViewTwo;

@property(nonatomic,strong)UITextView *textView;
@property(nonatomic,strong)UIButton *leftBtn;
@property(nonatomic,strong)UIButton *rightBtn;

@end
@implementation WPAlertView

/** 动画时间 */
static CGFloat const WP_animateWithDuration = 0.2;

- (instancetype)initWithTitle:(NSString *)title delegate:(id<WPAlertViewDelegate>)delegate contentTitle:(NSArray *)titles
{
    if (self = [super init])
    {
        self.frame = [UIScreen mainScreen].bounds;
        self.backgroundColor = [UIColor clearColor];
        
        self.messageTitle = title;
        self.delegate_SG = delegate;
        self.titles = [NSArray arrayWithArray:titles];
        
        [self setupAlertView];
    }
    return self;
}

- (void)setupAlertView {
    
    // 遮盖视图
    self.coverView = [[UIButton alloc] init];
    self.coverView.frame = self.bounds;
    self.coverView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
    [self addSubview:self.coverView];
    
    // 背景View
    self.bg_view = [[UIView alloc] init];
    _bg_view.layer.cornerRadius = 7;
    _bg_view.layer.masksToBounds = YES;
    _bg_view.backgroundColor = [UIColor whiteColor];
    [self.coverView addSubview:_bg_view];
    [_bg_view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.coverView).offset(42*K_SCALE_WIDTH);
        make.right.equalTo(self.coverView).offset(-42*K_SCALE_WIDTH);
        make.top.equalTo(self.coverView).offset(200*K_SCALE_FULL);
        make.height.mas_equalTo(190);
    }];
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.font = [UIFont systemFontOfSize:15];
    titleLabel.textColor = GLobalFontColor;
    titleLabel.text = self.messageTitle;
    [self.bg_view addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.bg_view);
        make.top.equalTo(self.bg_view).offset(18);
    }];
    
    self.textView = [[UITextView alloc] init];
    self.textView.editable = NO;
//    self.textView.backgroundColor = [UIColor orangeColor];
    
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    paraStyle.lineSpacing = UILABEL_LINE_SPACE; //设置行间距
    
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:14],NSForegroundColorAttributeName:GloballightFontColor, NSParagraphStyleAttributeName:paraStyle};
    
    NSString *str = self.titles[0];
    for (int i = 1; i < self.titles.count; i++)
    {
        str = [NSString stringWithFormat:@"%@\n%@",str,self.titles[i]];
    }
    
    NSAttributedString *attributeStr = [[NSAttributedString alloc] initWithString:str attributes:dic];
    self.textView.attributedText = attributeStr;
    
    [self.bg_view addSubview:self.textView];
    
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bg_view).offset(15);
        make.right.equalTo(self.bg_view).offset(-15);
        make.top.equalTo(titleLabel.mas_bottom).offset(15);
        make.bottom.equalTo(self.bg_view).offset(-50);
    }];
    
    UIView *bottomView = [UIView new];
    [self.bg_view addSubview:bottomView];
    bottomView.backgroundColor = GLobalLineColor;
    
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.bg_view);
        make.height.mas_equalTo(50);
    }];
    
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.leftBtn = leftButton;
    [leftButton setTitle:@"暂不更新" forState:UIControlStateNormal];
    [leftButton setTitleColor:GlobalColor forState:UIControlStateNormal];
    leftButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [leftButton setBackgroundColor:[UIColor whiteColor]];
    [leftButton addTarget:self action:@selector(buttonDidClick:) forControlEvents:UIControlEventTouchDown];
    [bottomView addSubview:leftButton];
    
    [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.equalTo(bottomView);
        make.top.equalTo(bottomView).offset(1);
        make.right.equalTo(bottomView.mas_centerX).offset(-0.5);
    }];
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.rightBtn = rightBtn;
    [rightBtn setTitle:@"立即升级" forState:UIControlStateNormal];
    [rightBtn setTitleColor:GlobalColor forState:UIControlStateNormal];
    rightBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [rightBtn setBackgroundColor:[UIColor whiteColor]];
    [rightBtn addTarget:self action:@selector(buttonDidClick:) forControlEvents:UIControlEventTouchDown];
    [bottomView addSubview:rightBtn];
    
    [rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.equalTo(bottomView);
        make.top.equalTo(bottomView).offset(1);
        make.left.equalTo(bottomView.mas_centerX).offset(0.5);
    }];
}

-(void)buttonDidClick:(UIButton *)btn
{
    [self dismiss];
    if (btn == self.rightBtn)
    {
        [[UIApplication sharedApplication] openURL:kAppStoreURL];
    }
}

- (void)show {
    if (self.superview != nil) return;
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    [keyWindow addSubview:self];
    
    [UIView animateWithDuration:WP_animateWithDuration animations:^{
        [self animationWithAlertView];
    }];
}

-(void)animationWithAlertView {
    CAKeyframeAnimation * animation;
    animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    animation.duration = 0.15;
    animation.removedOnCompletion = YES;
    animation.fillMode = kCAFillModeForwards;
    NSMutableArray *values = [NSMutableArray array];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.1, 1.1, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    animation.values = values;
    [self.bg_view.layer addAnimation:animation forKey:nil];
}

- (void)dismiss {
    
    [UIView animateWithDuration:WP_animateWithDuration animations:^{
        
    } completion:^(BOOL finished) {
        [self.coverView removeFromSuperview];
        [self removeFromSuperview];
    }];
}

@end
